<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="类中文名称" prop="nameZh">
        <el-input
          v-model="queryParams.nameZh"
          placeholder="请输入类中文名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类英文名称" prop="nameEn">
        <el-input
          v-model="queryParams.nameEn"
          placeholder="请输入类英文名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否普通检索知识库" prop="generalSearch">
        <el-select v-model="queryParams.generalSearch" placeholder="请选择是否普通检索知识库" clearable size="small">
          <el-option
            v-for="dict in dict.type.business_whether"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否高级检索知识库" prop="advancedSearch">
        <el-select v-model="queryParams.advancedSearch" placeholder="请选择是否高级检索知识库" clearable size="small">
          <el-option
            v-for="dict in dict.type.business_whether"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否首页快捷入口" prop="indexEntrance">
        <el-select v-model="queryParams.indexEntrance" placeholder="请选择是否首页快捷入口" clearable size="small">
          <el-option
            v-for="dict in dict.type.business_whether"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否首页导航入口" prop="indexNavigation">
        <el-select v-model="queryParams.indexNavigation" placeholder="请选择是否首页导航入口" clearable size="small">
          <el-option
            v-for="dict in dict.type.business_whether"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="所属资源库" prop="db">
        <el-select v-model="queryParams.db" placeholder="请选择所属资源库" clearable size="small">
          <el-option
            v-for="dict in dict.type.business_db"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:classes:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:classes:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:classes:remove']"
        >删除
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          :loading="exportLoading"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:classes:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-refresh"
          size="mini"
          :loading="refreshCountLoading"
          @click="handleRefreshCount"
          v-hasPermi="['system:classes:list']"
        >刷新资源数
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-view"
          size="mini"
          :disabled="multiple"
          :loading="releaseLoading"
          @click="handleRelease"
          v-hasPermi="['system:classes:release']"
        >发布
        </el-button>
      </el-col>
      <right-toolbar :columns.sync="columnsDisplay" :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" ref="table" :data="classesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column v-if="showColumn('唯一标识')" label="唯一标识" align="center" prop="id"/>
      <el-table-column v-if="showColumn('类中文名称')" label="类中文名称" align="center" prop="nameZh"/>
      <el-table-column v-if="showColumn('类英文名称') && checkPermi(['system:dataProperty:list'])" label="类英文名称" align="center" :show-overflow-tooltip="true" >
        <template slot-scope="scope" >
          <router-link :to="'/knowledgeBaseManagement/dataProperty/' + scope.row.id" class="link-type">
            <span>{{ scope.row.nameEn }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column v-else-if="showColumn('类英文名称')" label="类英文名称" align="center" prop="nameEn"/>
      <el-table-column v-if="showColumn('资源数')"  label="资源数" align="center" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <router-link :to="'/knowledgeBaseManagement/dynamic/' + scope.row.id" class="link-type">
            <span>{{ scope.row.resourceCount || 0 }}</span>
          </router-link>
        </template>
      </el-table-column>
      <el-table-column v-if="showColumn('排序')"  label="排序" align="center" prop="sort"/>
      <el-table-column v-if="showColumn('普通检索知识库')" label="普通检索知识库" align="center" prop="generalSearch">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_whether" :value="scope.row.generalSearch"/>
        </template>
      </el-table-column>
      <el-table-column v-if="showColumn('高级检索知识库')" label="高级检索知识库" align="center" prop="advancedSearch">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_whether" :value="scope.row.advancedSearch"/>
        </template>
      </el-table-column>
      <el-table-column v-if="showColumn('首页快捷入口')"  label="首页快捷入口" align="center" prop="indexEntrance">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_whether" :value="scope.row.indexEntrance"/>
        </template>
      </el-table-column>
      <el-table-column v-if="showColumn('首页导航入口')"  label="首页导航入口" align="center" prop="indexNavigation">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_whether" :value="scope.row.indexNavigation"/>
        </template>
      </el-table-column>
      <el-table-column v-if="showColumn('所属资源库')" label="所属资源库" align="center" prop="db">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.business_db" :value="scope.row.db"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="280px" class-name="small-padding fixed-width"
                       v-if="showColumn('操作') && checkPermi(['system:classes:edit', 'system:classes:remove', 'system:classes:release', 'system:classes:associate'])">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:classes:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:classes:remove']"
          >删除
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleRelease(scope.row)"
            v-hasPermi="['system:classes:release']"
          >发布
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-attract"
            @click="handleAssociate(scope.row)"
            v-hasPermi="['system:classes:associate']"
          >关联属性
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改类管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" :close-on-click-modal="false" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="auto" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="类中文名称" prop="nameZh">
              <el-input v-model="form.nameZh" placeholder="请输入类中文名称"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类英文名称" prop="nameEn">
              <el-input v-model="form.nameEn" placeholder="请输入类英文名称" :disabled="editDisabled"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="描述" prop="description">
              <el-input v-model="form.description" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="父类(只能选择根目录或一级目录)" prop="classesId">
              <el-select v-model="form.classesId" placeholder="请选择父类" :disabled="editDisabled">
                <el-option
                  v-for="item in classesOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="parseInt(item.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否普通检索知识库" prop="generalSearch">
              <el-select v-model="form.generalSearch" placeholder="请选择是否普通检索知识库">
                <el-option
                  v-for="dict in dict.type.business_whether"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否高级检索知识库" prop="advancedSearch">
              <el-select v-model="form.advancedSearch" placeholder="请选择是否高级检索知识库">
                <el-option
                  v-for="dict in dict.type.business_whether"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="是否首页快捷入口" prop="indexEntrance">
              <el-select v-model="form.indexEntrance" placeholder="请选择是否首页快捷入口">
                <el-option
                  v-for="dict in dict.type.business_whether"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否首页导航入口" prop="indexNavigation">
              <el-select v-model="form.indexNavigation" placeholder="请选择是否首页导航入口">
                <el-option
                  v-for="dict in dict.type.business_whether"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item prop="db">
               <span slot="label">
                <el-tooltip content="用于分资源库检索" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                所属资源库
              </span>
              <el-select v-model="form.db" placeholder="请选择所属资源库">
                <el-option
                  v-for="dict in dict.type.business_db"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="detailUrl">
                <span slot="label">
                <el-tooltip content="资源详情页的请求地址（暂未开发）" placement="top">
                <i class="el-icon-question"></i>
                </el-tooltip>
                详情地址
              </span>
              <el-input v-model="form.detailUrl" placeholder="请输入详情地址"/>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog title="关联属性管理" :visible.sync="openOp" width="80%"  :close-on-click-modal="false" append-to-body >
      <dialog-classes-op :classesIdSource="classesIdSource"></dialog-classes-op>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openOp = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import { checkPermi, checkRole } from "@/utils/permission";
    import {listClasses, getClasses, delClasses, addClasses, updateClasses, exportClasses, refreshResourceCount} from "@/api/system/classes";
    import {releaseClasses} from '@/api/system/dynamic'


    import dialogClassesOp from "../objectProperty/index";

    export default {
        name: "Classes",
        dicts: ['business_whether', 'business_whether', 'business_whether', 'business_whether', 'business_db'],
        components:{
            dialogClassesOp
        },
        data() {
            return {
                //打开关联属性是选择的类id
                classesIdSource: 0,
                //关联属性弹窗
                openOp: false,
                // 遮罩层
                loading: true,
                // 导出遮罩层
                exportLoading: false,
                // 导出遮罩层
                releaseLoading: false,
                refreshCountLoading: false,
                // 选中数组
                ids: [],
                // 非单个禁用
                single: true,
                // 非多个禁用
                multiple: true,
                // 显示搜索条件
                showSearch: true,
                // 总条数
                total: 0,
                // 类管理表格数据
                classesList: [],
                // 父类选择
                classesOptions: [{"value": 0, "label": "根目录"}],
                // 弹出层标题
                title: "",
                // 是否显示弹出层
                open: false,
                // 查询参数
                queryParams: {
                    pageNum: 1,
                    pageSize: 10,
                    nameEn: null,
                    nameZh: null,
                    display: null,
                    generalSearch: null,
                    advancedSearch: null,
                    indexEntrance: null,
                    indexNavigation: null,
                    db: null
                },
                // 表单参数
                form: {},
                // 表单校验
                rules: {
                    nameZh: [
                        { required: true,   message: '请输入中文名称', trigger: 'blur' },
                    ],
                    nameEn: [
                        { required: true,   message: '请输入英文名称', trigger: 'blur' },
                        { pattern: /^[a-z_]+$/,
                            message: "只允许小写字母、下划线组合",
                            trigger: "blur"
                        }
                    ],
                    sort: [
                        { required: true,   message: '请输入排序', trigger: 'blur' },
                        { pattern: /^[1-9]\d*$/,
                            message: "只允许正整数排序值",
                            trigger: "blur"
                        }
                    ],
                },
                editDisabled: false,
                columnsDisplay: []
            };
        },
        created() {
            this.getList();
        },
        mounted() {
            let columnsMap = this.$refs.table.columns;
            this.columnsDisplay = columnsMap.filter(x=>x.label).map(x=>{
                return {
                    label: x.label,
                    visible: true
                }
            });
        },
        computed: {
            showColumn(){
                return (col)=>{
                    if(!this.columnsDisplay.length){
                        return true;
                    }
                    let exist=this.columnsDisplay.filter(item=> item.label==col);
                    if(exist.length == 0){
                        return true
                    }else{
                        return exist[0].visible
                    }
                }
            },
        },
        methods: {
            checkPermi,
            checkRole,

            /** 查询类管理列表 */
            getList() {
                this.loading = true;
                listClasses(this.queryParams).then(response => {
                    this.classesList = response.rows;
                    this.total = response.total;
                    this.loading = false;
                    this.classesOptions = [{"value": 0, "label": "根目录"}];
                    if (this.classesList.length > 0) {
                        this.classesList.filter(x => x.classesId == 0).map(item => {
                            this.classesOptions.push({
                                "value": item.id,
                                "label": item.nameZh
                            });
                        });
                    }
                });
            },
            // 取消按钮
            cancel() {
                this.open = false;
                this.reset();
            },
            // 表单重置
            reset() {
                this.form = {
                    id: null,
                    nameEn: null,
                    nameZh: null,
                    description: null,
                    classesId: 0,
                    sort: null,
                    display: null,
                    generalSearch: "1",
                    advancedSearch: "1",
                    indexEntrance: "0",
                    indexNavigation: "0",
                    detailUrl: null,
                    db: null
                };
                this.resetForm("form");
            },
            /** 搜索按钮操作 */
            handleQuery() {
                this.queryParams.pageNum = 1;
                this.getList();
            },
            /** 重置按钮操作 */
            resetQuery() {
                this.resetForm("queryForm");
                this.handleQuery();
            },
            // 多选框选中数据
            handleSelectionChange(selection) {
                this.ids = selection.map(item => item.id)
                this.single = selection.length !== 1
                this.multiple = !selection.length
            },
            /** 管理关联属性 */
            handleAssociate(row) {
              this.classesIdSource = row.id;
              this.openOp = true;
            },
            /** 新增按钮操作 */
            handleAdd() {
                this.reset();
                this.open = true;
                this.editDisabled = false;
                this.title = "添加类管理";
            },
            /** 修改按钮操作 */
            handleUpdate(row) {
                console.log(this.classesOptions)
                this.reset();
                const id = row.id || this.ids
                getClasses(id).then(response => {
                    this.form = response.data;
                    this.open = true;
                    this.editDisabled = true;
                    this.title = "修改类管理";
                });
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs["form"].validate(valid => {
                    if (valid) {
                        if (this.form.id != null) {
                            updateClasses(this.form).then(response => {
                                this.$modal.msgSuccess("修改成功");
                                debugger
                                this.open = false;
                                this.getList();
                            });
                        } else {
                            addClasses(this.form).then(response => {
                                this.$modal.msgSuccess("新增成功");
                                debugger
                                this.open = false;
                                this.getList();
                            });
                        }
                    }
                });
            },
            /** 删除按钮操作 */
            handleDelete(row) {
                const ids = row.id || this.ids;
                this.$modal.confirm('是否确认删除类管理编号为"' + ids + '"的数据项？').then(function () {
                    return delClasses(ids);
                }).then(() => {
                    this.getList();
                    this.$modal.msgSuccess("删除成功");
                }).catch(() => {
                });
            },
            /** 导出按钮操作 */
            handleExport() {
                const queryParams = this.queryParams;
                this.$modal.confirm('是否确认导出所有类管理数据项？').then(() => {
                    this.exportLoading = true;
                    return exportClasses(queryParams);
                }).then(response => {
                    this.$download.name(response.msg);
                    this.exportLoading = false;
                }).catch(() => {
                });
            },
            handleRefreshCount() {
                this.$modal.confirm('是否确认刷新资源数？').then(() => {
                    this.refreshCountLoading = true;
                    return refreshResourceCount()
                }).then(response => {
                    this.refreshCountLoading = false;
                    this.$modal.msgSuccess("刷新成功");
                    this.getList();
                }).catch(() => {
                });
            },
            handleRelease(row) {
                let idsArr = [];
                if (row.id) {
                    idsArr.push(row.id)
                }
                if (this.ids.length > 0) {
                    idsArr = idsArr.concat(this.ids);
                }
                if (idsArr.length > 0) {
                    const data = {
                        classesIds: idsArr
                    };
                    this.$modal.confirm('是否确认发布类管理数据项？').then(() => {
                        this.releaseLoading = true;
                        return releaseClasses(data)
                    }).then(response => {
                        this.releaseLoading = false;
                        this.$modal.msgSuccess("发布成功");
                    }).catch(() => {
                    });
                } else {
                    this.$modal.msgWarning("请选择需要发布类管理数据项!");
                }
            }
        }
    };
</script>
<style>
  .el-form-item .el-select {
    width: 100%;
  }

  .el-form--label-top .el-form-item__label {
    padding: 0 !important;
  }

  .el-row {
    margin-bottom: 0px;
  }

  .el-row:last-child {
    margin-bottom: 0;
  }

  .el-col {
    border-radius: 4px;
  }

  .el-form--label-top .el-form-item__label {
    float: none;
    display: inline-block;
    text-align: left;
    padding: 0;
  }

</style>
